package P1线性结构.LeetCode双指针;

import java.util.HashSet;
import java.util.TreeSet;

public class LeetCode26 {
    public static void main(String[] args) {
        //去重复元素->set TreeSet HashSet
        int[] arr = {0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 4};
        HashSet<Integer> set = new HashSet<>();
        //0 1 2 3 4
        for (int i = 0; i < arr.length; i++) {
            set.add(arr[i]);
        }
        System.out.println(set.size());
        for (Integer num : set) {
            System.out.println(num);
        }

        test();
    }

    private static void test() {
        HashSet<String> set1 = new HashSet<>();
        //TreeSet会根据元素自身进行自然排序
        TreeSet<String> set2 = new TreeSet<>();
        String[] arr = {"GHF","KJH","ABC","OPI","BCD","GHJ"};
        for (int i = 0; i < arr.length; i++) {
            set1.add(arr[i]);
            set2.add(arr[i]);
        }
        for (String s : set1) {
            System.out.print(s + ",");
        }
        System.out.println();
        for (String s : set2) {
            System.out.print(s + ",");
        }
        System.out.println();
    }

    public int removeDuplicates(int[] nums) {
        //O(n)
        int j = 0;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != nums[j]) {
                j++;
                nums[j] = nums[i];
            }
        }
        return j + 1;
    }
}
